package secondStage.part3.List集合;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @ClassName: [ListDemo2]
 * @Description: List集合子类的特点
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-03]
 * @Version: [v1.0]
 */
/*
List集合子类特点
List集合常用子类: ArrayList，LinkedList
ArrayList:底层数据结构是数组，查询快，增删慢
LinkedList:底层数据结构是链表，查询慢，增删快
练习：
    分别使用ArrayList和LinkedList完成存储字符串并遍历*/
public class ListDemo2 {
    public static void main(String[] args) {
        //创建ArrayList集合对象
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("hello");
        arrayList.add("world");
        arrayList.add("Java");
        //增强for循环遍历
        for (String s : arrayList) {
            System.out.println(s);
        }
        //迭代器遍历
        System.out.println("----迭代器遍历----");
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        //for循环遍历输出
        System.out.println("----for循环----");
        for (int i = 0; i < arrayList.size(); i++) {
            String s = arrayList.get(i);
            System.out.println(s);
        }
        System.out.println("LinkedList特点：");
        //创建LinkedList集合对象
        LinkedList<String> linkedList = new LinkedList<>();

        linkedList.add("hello");
        linkedList.add("word");
        linkedList.add("Javaee");
        for (String s : linkedList) {
            System.out.println(s);
        }
        System.out.println("------");
        Iterator<String> its = linkedList.iterator();
        while (its.hasNext()) {
            String s = its.next();
            System.out.println(s);
        }
        System.out.println("------");
        for (int i = 0; i < linkedList.size(); i++) {
            String s = linkedList.get(i);
            System.out.println(s);
        }
    }
}
